﻿using BLL;
using BLL.Access;
using Entity;
using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Web.Class;

namespace Web.Controls.LAIHAN
{
    public partial class UCLHShenQingXX_FH : System.Web.UI.UserControl, IUCLHOperate
    {
        #region 属性

        /// <summary>
        /// 来函ID
        /// </summary>
        public string LaiHanID
        {
            get { return hdCollection.Contains("LaiHanID") ? hdCollection.Get("LaiHanID").ToString() : ""; }
            set { hdCollection.Set("LaiHanID", value); }
        }

        /// <summary>
        /// 来函编号
        /// </summary>
        public eLaiHanLCBH LaiHanBH { get; set; }

        /// <summary>
        /// 来函类型
        /// </summary>
        public eLaiHanLX LaiHanLX { get; set; }

        /// <summary>
        /// 来函流程
        /// </summary>
        public eLaiHanLCBH LaiHanLC { get; set; }

        /// <summary>
        /// 原用户号
        /// </summary>
        public string YYongHuH
        {
            get { return hdCollection.Contains("YongHuH") ? hdCollection.Get("YongHuH").ToString() : ""; }
            set { hdCollection.Set("YongHuH", value); }
        }
        /// <summary>
        /// 记录查询结果集
        /// </summary>
        private DataTable dtSource
        {
            get
            {
                return ViewState["dtSource"] as DataTable;
            }
            set
            {
                ViewState["dtSource"] = value;
            }
        }
        /// <summary>
        /// 记录原用户信息
        /// </summary>
        private DataTable dtYYongHu
        {
            get
            {
                return ViewState["dtYYongHu"] as DataTable;
            }
            set
            {
                ViewState["dtYYongHu"] = value;
            }
        }

        /// <summary>
        /// 记录站点
        /// </summary>
        public string ZhanDianBH
        {
            get
            {
                return Convert.ToString(ViewState["ZhanDianBH"]);
            }
            set
            {
                ViewState["ZhanDianBH"] = value;
            }
        }

        /// <summary>
        /// 记录管理块
        /// </summary>
        public string GuanLiKBH
        {
            get
            {
                return Convert.ToString(ViewState["GuanLiKBH"]);
            }
            set
            {
                ViewState["GuanLiKBH"] = value;
            }
        }
        #endregion

        #region 变量
        /// <summary>
        /// 流程信息BLl
        /// </summary>
        YS_LH_LAIHANLCJLBLL bllLCJL = new YS_LH_LAIHANLCJLBLL();
        /// <summary>
        /// 来函分户信息
        /// </summary>
        YS_LH_FENHUXXBLL bllFenHuXX = new YS_LH_FENHUXXBLL();
        /// <summary>
        /// 用户信息BLL
        /// </summary>
        YS_YG_YONGHUXXBLL bllYongHu = new YS_YG_YONGHUXXBLL();
        /// <summary>
        /// 原计划BLL
        /// </summary>
        YS_JH_YUANJIHUABLL bllYuanJH = new YS_JH_YUANJIHUABLL();
        /// <summary>
        /// 存储过程BLL
        /// </summary>
        SpHelper _spHelper = new SpHelper();
        /// <summary>
        /// 来函信息
        /// </summary>
        YS_LH_LAIHANXXProcess bllLaiHanXX = new YS_LH_LAIHANXXProcess();
        /// <summary>
        /// 来函用户信息
        /// </summary>
        YS_LH_YONGHUTZProcess bllLaiHanYHTZ = new YS_LH_YONGHUTZProcess();
        /// <summary>
        /// 用户水表关系BLL
        /// </summary>
        YS_LH_YONGHUSBGXProcess bllYongHuSBGX = new YS_LH_YONGHUSBGXProcess();
        /// <summary>
        /// 水表信息BLL
        /// </summary>
        YS_CB_SHUIBIAOXXBLL bllShuiBiaoXX = new YS_CB_SHUIBIAOXXBLL();

        /// <summary>
        /// 日志BLL
        /// </summary>
        SYS_LOGBLL _SYS_LOGBLL = new SYS_LOGBLL();
        #endregion

        #region 事件
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                InitControl();
                ClearSession();
            }
            SetDisplay();
        }
        protected void ASPxCallback1_Callback(object source, DevExpress.Web.CallbackEventArgs e)
        {
            if (e.Parameter == "yonghuh")
            {
                e.Result = e.Parameter + "|" + LoadYongHuXX(0);
            }
            else if (e.Parameter == "hth")
            {
                //获取合同号
                string hetongh = BusinessTool.GetHeTongH(this.txtYongHuH.Text);
                e.Result = e.Parameter + "|0|"+ hetongh;
            }
            else if(e.Parameter == "setinfo")
            {
                string msg = "";
                int index = gvList.FocusedRowIndex;
                if (index >= 0)
                {
                    msg = string.Join("|", gvList.GetRowValues(index, "S_YONGHUH").ToString(), gvList.GetRowValues(index, "S_YONGHUM").ToString(),
                        gvList.GetRowValues(index, "S_DIZHI").ToString(), gvList.GetRowValues(index, "S_JUBIANH").ToString(), gvList.GetRowValues(index, "S_GONGSIBH").ToString(),
                        gvList.GetRowValues(index, "S_XINGZHENGQBH").ToString(), gvList.GetRowValues(index, "S_YEWUY").ToString(), gvList.GetRowValues(index, "S_LIANXIR").ToString(),
                        gvList.GetRowValues(index, "S_LIANXIDH").ToString(), gvList.GetRowValues(index, "S_LIANXIBM").ToString(), gvList.GetRowValues(index, "S_YOUBIAN").ToString(),
                        gvList.GetRowValues(index, "S_YONGSHUIXZ").ToString(), gvList.GetRowValues(index, "S_SHESHUIDXDM").ToString(), gvList.GetRowValues(index, "I_KAOHEFY").ToString(),
                        gvList.GetRowValues(index, "N_YSJ").ToString(), gvList.GetRowValues(index, "S_KAIHUHM").ToString(), gvList.GetRowValues(index, "S_KAIHUYH").ToString(),
                        gvList.GetRowValues(index, "S_YINHANGZH").ToString(), gvList.GetRowValues(index, "I_SHOUFEIFS").ToString(), gvList.GetRowValues(index, "S_HETONGH").ToString());
                    e.Result = e.Parameter + "|0|" + msg;
                }
                else
                {
                    e.Result = e.Parameter + "|-1|" + msg;
                }
            }
        }

        protected void cmbGongSi_Callback(object sender, DevExpress.Web.CallbackEventArgsBase e)
        {
            if (this.cmbJu.Value == null)
            {
                this.cmbGongSi.Items.Clear();
            }
            else
            {
                PageTools.LoadComboxGongSi(this.cmbGongSi, this.cmbJu.Value.ToString(), null);
            }
        }

        protected void BtnAddYH_Click(object sender, EventArgs e)
        {
            var rs = FenHuValidate();
            if (!rs.IsSuccess)
            {
                PageTools.AlertMsg(this, rs.Msg, "");
                return;
            }
            if (this.dtSource == null)
            {
                this.dtSource = new DataTable();
                this.dtSource.Columns.Add("S_YYONGHUH");
                this.dtSource.Columns.Add("S_YONGHUH");
                this.dtSource.Columns.Add("S_YONGHUM");
                this.dtSource.Columns.Add("S_DIZHI");
                this.dtSource.Columns.Add("S_JUBIANH");
                this.dtSource.Columns.Add("S_GONGSIBH");
                this.dtSource.Columns.Add("S_XINGZHENGQBH");
                this.dtSource.Columns.Add("S_YEWUY");
                this.dtSource.Columns.Add("S_LIANXIR");
                this.dtSource.Columns.Add("S_LIANXIDH");
                this.dtSource.Columns.Add("S_LIANXIBM");
                this.dtSource.Columns.Add("S_YOUBIAN");
                this.dtSource.Columns.Add("S_YONGSHUIXZ");
                this.dtSource.Columns.Add("S_SHESHUIDXDM");
                this.dtSource.Columns.Add("N_YSJ");
                this.dtSource.Columns.Add("I_KAOHEFY");
                this.dtSource.Columns.Add("S_KAIHUHM");
                this.dtSource.Columns.Add("S_KAIHUYH");
                this.dtSource.Columns.Add("S_YINHANGZH");
                this.dtSource.Columns.Add("I_SHOUFEIFS");
                this.dtSource.Columns.Add("S_HETONGH");
                this.dtSource.Columns.Add("I_M1");
                this.dtSource.Columns.Add("I_M2");
                this.dtSource.Columns.Add("I_M3");
                this.dtSource.Columns.Add("I_M4");
                this.dtSource.Columns.Add("I_M5");
                this.dtSource.Columns.Add("I_M6");
                this.dtSource.Columns.Add("I_M7");
                this.dtSource.Columns.Add("I_M8");
                this.dtSource.Columns.Add("I_M9");
                this.dtSource.Columns.Add("I_M10");
                this.dtSource.Columns.Add("I_M11");
                this.dtSource.Columns.Add("I_M12");
                this.dtSource.Columns.Add("I_M13");
            }
            DataRow dr = this.dtSource.NewRow();
            dr["S_YYONGHUH"] = hdCollection.Get("YongHuH");
            dr["S_YONGHUH"] = txtYongHuH.Text.Trim();
            dr["S_YONGHUM"] = txtYongHuM.Text.Trim();
            dr["S_DIZHI"] = txtDiZhi.Text.Trim();
            dr["S_JUBIANH"] = cmbJu.Value == null ? "" : cmbJu.Value.ToString();
            dr["S_GONGSIBH"] = cmbGongSi.Value == null ? "" : cmbGongSi.Value.ToString(); ;
            dr["S_XINGZHENGQBH"] = cmbXingZhengQ.Value.ToString();
            dr["S_YEWUY"] = cmbYWY.Value.ToString();
            dr["S_LIANXIR"] = txtLianXiR.Text;
            dr["S_LIANXIDH"] = txtLianXiDH.Text;
            dr["S_LIANXIBM"] = txtLianXiBM.Text;
            dr["S_YOUBIAN"] = txtYouBian.Text;
            dr["S_YONGSHUIXZ"] = cmbYongShuiXZ.Value.ToString();
            dr["S_SHESHUIDXDM"] = cmbHangYeLX.Value.ToString();
            dr["N_YSJ"] = Convert.ToDouble(txtYSJ.Text);
            dr["I_KAOHEFY"] = Convert.ToInt32(cmbKaoHeFY.Value);
            dr["S_KAIHUHM"] = txtKaiHuM.Text;
            dr["S_KAIHUYH"] = txtKaiHuYH.Text;
            dr["S_YINHANGZH"] = txtKaiHuZH.Text;
            dr["I_SHOUFEIFS"] = (cbTuoShou.Checked == true ? 1 : 0);
            string HeTongH = _spHelper.P_YS_GETHETONGH(txtYongHuH.Text.Trim()).Tables[0].Rows[0][0].ToString();
            dr["S_HETONGH"] = txtHeTongH.Text==""? HeTongH:txtHeTongH.Text;
            dr["I_M1"] = Convert.ToInt32(txtFenHu1JH1.Text);
            dr["I_M2"] = Convert.ToInt32(txtFenHu1JH2.Text);
            dr["I_M3"] = Convert.ToInt32(txtFenHu1JH3.Text);
            dr["I_M4"] = Convert.ToInt32(txtFenHu1JH4.Text);
            dr["I_M5"] = Convert.ToInt32(txtFenHu1JH5.Text);
            dr["I_M6"] = Convert.ToInt32(txtFenHu1JH6.Text);
            dr["I_M7"] = Convert.ToInt32(txtFenHu1JH7.Text);
            dr["I_M8"] = Convert.ToInt32(txtFenHu1JH8.Text);
            dr["I_M9"] = Convert.ToInt32(txtFenHu1JH9.Text);
            dr["I_M10"] = Convert.ToInt32(txtFenHu1JH10.Text);
            dr["I_M11"] = Convert.ToInt32(txtFenHu1JH11.Text);
            dr["I_M12"] = Convert.ToInt32(txtFenHu1JH12.Text);
            dr["I_M13"] = Convert.ToInt32(txtFenHu1JH13.Text);
            this.dtSource.Rows.Add(dr);
            this.gvList.DataSource = this.dtSource;
            this.gvList.DataBind();
            this.txtYYongHuH.ReadOnly = true;
            SetYongHuXX(dtYYongHu);
            SetJiHuaXX(hdCollection.Get("YongHuH").ToString());
        }

        protected void BtnAddSB_Click(object sender, EventArgs e)
        {
            if (this.dtSource == null || this.dtSource.Rows.Count == 0)
            {
                PageTools.AlertMsg(this, "请添加分户");
                return;
            }
            Session["dtFenHuList"] = this.dtSource;
            string type = "0";
            if(this.LaiHanLC > eLaiHanLCBH.预登记)
            {
                type = "1";
            }
            string script = "OpenShuiBiaoXX('{0}','{1}','{2}');";
            script = string.Format(script, hdCollection.Get("YongHuH"), type, !string.IsNullOrEmpty(Request["id"]) ? Request["id"] : "");
            PageTools.ResponseScript(this, script);
            
        }

        /// <summary>
        /// 用户搜索
        /// </summary>
        protected void BtnSearch_Click(object sender, EventArgs e)
        {
            LoadYongHuXX(1); 
            txtYongHuH.Focus();
        }

        /// <summary>
        /// 清空
        /// </summary>
        protected void BtnClear_Click(object sender, EventArgs e)
        {
            ClearInfo();
            txtYYongHuH.Focus();
        }

        /// <summary>
        /// 移除已添加分户
        /// </summary>
        protected void lnbDel_Command(object sender, CommandEventArgs e)
        {
            //清除结果集中对应记录
            for (int i = 0; i < this.dtSource.Rows.Count; i++)
            {
                if (this.dtSource.Rows[i]["S_YONGHUH"].ToString() == e.CommandArgument.ToString())
                {
                    this.dtSource.Rows.RemoveAt(i);
                }
            }
            this.gvList.DataSource = this.dtSource;
            this.gvList.DataBind();
            SetYongHuXX(dtYYongHu);
            SetJiHuaXX(hdCollection.Get("YongHuH").ToString());
        }
        #endregion

        #region 方法
        /// <summary>
        /// 配置控件
        /// </summary>
        public void InitControl()
        {
            //根据操作人加载管理部门(站点)
            DataTable dtZhanDian = BusinessTool.GetTextAndValue("View_AS_UserInfo", "ZHANDIANMC", "ZHANDIANBH", "-", string.Format("USERID={0}", LoginInfo.CurrentUser.UserID)).Tables[0];
            string zhandianBH = dtZhanDian.Rows[0]["VALUE"].ToString();

            PageTools.LoadCombox(this.cmbXingZhengQ, PageTools.eLoadType.行政区, BusinessTool.ZHANDIANBH(), null, "");
            PageTools.LoadCombox(this.cmbJu, PageTools.eLoadType.局, BusinessTool.ZHANDIANBH(), null, "");
            if (zhandianBH != "51" && zhandianBH != "00")
            {
                this.cmbJu.ReadOnly = true;
            }
            //加载业务员
            DataTable YeWuY = BusinessTool.GetTextAndValue(
                    "View_AS_UserInfo", "USERNAME", "USERID", null, string.Format("(ZHANDIANBH='{0}' or '00'='{0}') and ROLENAME like '%业务员%'", zhandianBH)
                ).Tables[0];
            PageTools.BindComboBox(
                this.cmbYWY, YeWuY
            );
            //加载用水性质
            DataTable YongShuiXZ = BusinessTool.GetTextAndValue(
                 "YS_JX_YONGSHUIXZ", "S_MINGCHENG", "S_BIANMA", "-", string.Format("I_TYPE={0}", zhandianBH.Contains("51") ? 1 : 0)
                 ).Tables[0];
            PageTools.BindComboBox(
                 this.cmbYongShuiXZ, YongShuiXZ
             );
            if (this.cmbYongShuiXZ.Items.Count != 0)
            {
                LoadGongSiByJu();
            }
            PageTools.LoadCombox(this.cmbHangYeLX, PageTools.eLoadType.行业类型, BusinessTool.ZHANDIANBH(), null, null);
            PageTools.LoadCombox(this.cmbKaoHeFY, PageTools.eLoadType.考核复用, BusinessTool.ZHANDIANBH(), "-", "");
            
            //加载用户、计划信息
            YS_LH_LAIHANXX laihanxx = null;
            //加载申请信息
            if (!string.IsNullOrEmpty(Request["id"]))
            {
                laihanxx = new YS_LH_LAIHANXXProcess().SelectYS_LH_LAIHANXX(decimal.Parse(Request["id"]));
            }
            else
            {
                laihanxx = new YS_LH_LAIHANXX();
            }
            LoadInfo(laihanxx);
        }

        /// <summary>
        /// 根据局信息加载公司信息
        /// </summary>
        /// <param name="type"></param>
        private void LoadGongSiByJu()
        {
            string juhao = "";
            if (cmbJu.Value == null)
            {
                return;
            }
            juhao = cmbJu.Value.ToString();
            
            DataTable table = BusinessTool.GetTextAndValue(
                       "YS_JX_GONGSIXX", "S_MINGCHENG", "S_BIANHAO", null, string.Format("S_JUBIANH='{0}' and I_LEIXING=0", juhao)
                   ).Tables[0];
            if (table.Rows.Count > 0)
            {
                PageTools.BindComboBox(this.cmbGongSi, table);
            }
            else
            {
                this.cmbGongSi.Items.Clear();
            }
        }

        /// <summary>
        /// 情况水表分配数据
        /// </summary>
        private void ClearSession()
        {
            Session["DtFenHuSB"] = null;
        }

        /// <summary>
        ///根据当前流程状态设置步骤显示
        /// </summary>
        private void SetDisplay()
        {
            //先隐藏所有
            this.gvList.Visible = false;
            this.tabUserInfo.Visible = false;
            this.tabJiHua.Visible = false;
            this.tabBtnAdd.Visible = false;
            //根据来函显示面板
            if (this.LaiHanLC <= eLaiHanLCBH.预登记)
            {
                this.gvList.Visible = true;
                this.gvList.Columns[0].Visible = true;
                this.tabUserInfo.Visible = true;
                this.tabJiHua.Visible = true;
                this.tabBtnAdd.Visible = true;
                this.BtnAddYH.Enabled = true;
                this.BtnAddSB.Enabled = true;
                if(Request["id"] == null)
                {
                    txtYYongHuH.ClientEnabled = true;
                    txtYYongHuH.Focus();
                }
                else
                {
                    txtYYongHuH.ClientEnabled = false;
                }
            }
            else
            {
                this.gvList.Visible = true;
                this.gvList.Columns[0].Visible = false;
                this.tabUserInfo.Visible = true;
                this.tabJiHua.Visible = true;
                this.tabBtnAdd.Visible = true;
                this.BtnAddYH.Enabled = false;
                this.BtnAddSB.Enabled = false;
                txtYYongHuH.ClientEnabled = false;
            }
        }

        /// <summary>
        /// 添加分户信息验证
        /// </summary>
        /// <returns></returns>
        public ValidateRS FenHuValidate()
        {
            ValidateRS rs = new ValidateRS();
            rs.IsSuccess = false;
            if (txtYYongHuH.Text.Trim().Length == 0)
            {
                rs.Msg = "用户号不能为空";
                txtYYongHuH.Focus();
                return rs;
            }
            if (txtYongHuH.Text.Trim().Length == 0)
            {
                rs.Msg = "新用户号不能为空";
                txtYongHuH.Focus();
                return rs;
            }
            string msg = "";
            if (!CheckYongHuHIsUsed(out msg))
            {
                rs.Msg = msg;
                txtYongHuH.Focus();
                return rs;
            }
            if (dtSource != null && dtSource.Rows.Count > 0)
            {
                for (int i = 0; i < dtSource.Rows.Count; i++)
                {
                    if (txtYongHuH.Text.Trim() == dtSource.Rows[i]["S_YONGHUH"].ToString())
                    {
                        rs.Msg = "用户号不能重复";
                        txtYongHuH.Focus();
                        return rs;
                    }
                }
            }
            if (txtYongHuM.Text.Trim().Length == 0)
            {
                rs.Msg = "用户名不能为空";
                txtYongHuM.Focus();
                return rs;
            }
            if (txtDiZhi.Text.Trim().Length == 0)
            {
                rs.Msg = "地址不能为空";
                txtDiZhi.Focus();
                return rs;
            }
            if (cmbXingZhengQ.Value == null || cmbXingZhengQ.Value.ToString().Trim().Length == 0)
            {
                rs.Msg = "请选择行政区";
                cmbXingZhengQ.Focus();
                return rs;
            }
            if (cmbYWY.Value == null || cmbYWY.Value.ToString().Trim().Length == 0)
            {
                rs.Msg = "请选择业务员";
                cmbYWY.Focus();
                return rs;
            }
            if (txtLianXiR.Text.Trim().Length == 0)
            {
                rs.Msg = "联系人不能为空";
                txtLianXiR.Focus();
                return rs;
            }
            if (txtLianXiDH.Text.Trim().Length == 0)
            {
                rs.Msg = "联系电话不能为空";
                txtLianXiDH.Focus();
                return rs;
            }
            if (txtLianXiBM.Text.Trim().Length == 0)
            {
                rs.Msg = "联系部门不能为空";
                txtLianXiBM.Focus();
                return rs;
            }
            if (txtYouBian.Text.Trim().Length == 0)
            {
                rs.Msg = "邮编不能为空";
                txtYouBian.Focus();
                return rs;
            }
            if (cmbYongShuiXZ.Value == null || cmbYongShuiXZ.Value.ToString().Trim().Length == 0)
            {
                rs.Msg = "请选择用水性质";
                cmbYongShuiXZ.Focus();
                return rs;
            }
            if (cmbHangYeLX.Value == null || cmbHangYeLX.Value.ToString().Trim().Length == 0)
            {
                rs.Msg = "请选择行业类别";
                cmbHangYeLX.Focus();
                return rs;
            }
            if (txtYSJ.Text.Trim().Length == 0 || Convert.ToDouble(txtYSJ.Text) < 0)
            {
                rs.Msg = "用水价不能为空";
                txtYSJ.Focus();
                return rs;
            }
            if (cmbKaoHeFY.Value == null || cmbKaoHeFY.Value.ToString().Trim().Length == 0)
            {
                rs.Msg = "请选择考核复用";
                cmbKaoHeFY.Focus();
                return rs;
            }
            if (cbTuoShou.Checked)
            {
                if (txtKaiHuM.Text.Trim().Length == 0)
                {
                    rs.Msg = "开户户名不能为空";
                    txtKaiHuM.Focus(); return rs;
                }
                if (txtKaiHuYH.Text.Trim().Length == 0)
                {
                    rs.Msg = "开户银行不能为空";
                    txtKaiHuYH.Focus(); return rs;
                }
                if (txtKaiHuZH.Text.Trim().Length == 0)
                {
                    rs.Msg = "开户账号不能为空";
                    txtKaiHuZH.Focus(); return rs;
                }
                if (txtHeTongH.Text.Trim().Length == 0)
                {
                    rs.Msg = "合同号不能为空";
                    txtHeTongH.Focus(); return rs;
                }
            }
            //计划信息
            string msg2 = "";
            if (!CheckJiHuaXX(out msg2))
            {
                rs.Msg = msg;
                return rs;
            }
            
            rs.IsSuccess = true;
            return rs;
        }

        /// <summary>
        /// 验证提交
        /// </summary>
        /// <returns></returns>
        public ValidateRS Validate()
        {
            ValidateRS rs = new ValidateRS();
            rs.IsSuccess = false;
            if (txtYYongHuH.Text.Trim().Length == 0)
            {
                rs.Msg = "用户号不能为空";
                txtYYongHuH.Focus();
                return rs;
            }
            if(dtSource == null || dtSource.Rows.Count == 0)
            {
                rs.Msg = "分户不能为空";
                txtYongHuH.Focus();
                return rs;
            }
            
            rs.IsSuccess = true;
            return rs;
        }
        /// <summary>
        /// 判断用户号是否可用
        /// </summary>
        /// <param name="msg"></param>
        /// <returns></returns>
        public bool CheckYongHuHIsUsed(out string msg)
        {
            msg = "";
            if (txtYongHuH.Text.Trim().Substring(0, 2) != txtYYongHuH.Text.Trim().Substring(0, 2))
            {
                msg = "分户用户号格式错误（前两位为管理块号）";
                return false;
            }
            if (txtYongHuH.Text.Trim().Length != 7)
            {
                msg = "分户用户号应该为7位";
                return false;
            }
            string XiuGaiWhere = "";
            if (!string.IsNullOrEmpty(LaiHanID) && LaiHanID != "" && LaiHanID != "0")
            {
                XiuGaiWhere = " and S_YUANYONGHUH<>'{0}'";
                XiuGaiWhere = string.Format(XiuGaiWhere, hdCollection.Get("YongHuH").ToString());
            }
            string where = string.Format(" S_YONGHUH='{0}' and i_JLZT=0 ", txtYongHuH.Text.Trim());
            DataTable dt1 = bllYongHu.SelectYS_YG_YONGHUXXesDynamic(where, "", null);
            DataTable dt11 = bllLaiHanYHTZ.SelectYS_LH_YONGHUTZsDynamic(where + XiuGaiWhere, "", null);
            if ((dt1.Rows.Count > 0 || dt11.Rows.Count > 0))
            {
                msg = "用户号已存在";
                return false;

            }
            return true;
        }

        /// <summary>
        /// 判断计划信息
        /// </summary>
        /// <param name="msg"></param>
        /// <returns></returns>
        public bool CheckJiHuaXX(out string msg)
        {
            msg = "";
            int val = 0;
            if (!int.TryParse(txtFenHu1JH1.Text, out val))
            {
                txtFenHu1JH1.Focus();
                msg = "分户1月计划信息输入错误";
                return false;
            }
            else if (Convert.ToInt32(txtFenHu1JH1.Text) < 500)
            {
                txtFenHu1JH1.Focus();
                msg = "单月计划量最低为500";
                return false;
            }
            if (!int.TryParse(txtFenHu1JH2.Text, out val))
            {
                txtFenHu1JH2.Focus();
                msg = "分户2月计划信息输入错误";
                return false;
            }
            else if (Convert.ToInt32(txtFenHu1JH2.Text) < 500)
            {
                txtFenHu1JH2.Focus();
                msg = "单月计划量最低为500";
                return false;
            }
            if (!int.TryParse(txtFenHu1JH3.Text, out val))
            {
                txtFenHu1JH3.Focus();
                msg = "分户1月计划信息输入错误";
                return false;
            }
            else if (Convert.ToInt32(txtFenHu1JH3.Text) < 500)
            {
                txtFenHu1JH3.Focus();
                msg = "单月计划量最低为500";
                return false;
            }
            if (!int.TryParse(txtFenHu1JH4.Text, out val))
            {
                txtFenHu1JH4.Focus();
                msg = "分户1月计划信息输入错误";
                return false;
            }
            else if (Convert.ToInt32(txtFenHu1JH4.Text) < 500)
            {
                txtFenHu1JH4.Focus();
                msg = "单月计划量最低为500";
                return false;
            }
            if (!int.TryParse(txtFenHu1JH5.Text, out val))
            {
                txtFenHu1JH5.Focus();
                msg = "分户1月计划信息输入错误";
                return false;
            }
            else if (Convert.ToInt32(txtFenHu1JH5.Text) < 500)
            {
                txtFenHu1JH5.Focus();
                msg = "单月计划量最低为500";
                return false;
            }
            if (!int.TryParse(txtFenHu1JH6.Text, out val))
            {
                txtFenHu1JH6.Focus();
                msg = "分户1月计划信息输入错误";
                return false;
            }
            else if (Convert.ToInt32(txtFenHu1JH6.Text) < 500)
            {
                txtFenHu1JH6.Focus();
                msg = "单月计划量最低为500";
                return false;
            }
            if (!int.TryParse(txtFenHu1JH7.Text, out val))
            {
                txtFenHu1JH7.Focus();
                msg = "分户1月计划信息输入错误";
                return false;
            }
            else if (Convert.ToInt32(txtFenHu1JH7.Text) < 500)
            {
                txtFenHu1JH7.Focus();
                msg = "单月计划量最低为500";
                return false;
            }
            if (!int.TryParse(txtFenHu1JH8.Text, out val))
            {
                txtFenHu1JH8.Focus();
                msg = "分户1月计划信息输入错误";
                return false;
            }
            else if (Convert.ToInt32(txtFenHu1JH8.Text) < 500)
            {
                txtFenHu1JH8.Focus();
                msg = "单月计划量最低为500";
                return false;
            }
            if (!int.TryParse(txtFenHu1JH9.Text, out val))
            {
                txtFenHu1JH9.Focus();
                msg = "分户1月计划信息输入错误";
                return false;
            }
            else if (Convert.ToInt32(txtFenHu1JH9.Text) < 500)
            {
                txtFenHu1JH9.Focus();
                msg = "单月计划量最低为500";
                return false;
            }
            if (!int.TryParse(txtFenHu1JH10.Text, out val))
            {
                txtFenHu1JH10.Focus();
                msg = "分户1月计划信息输入错误";
                return false;
            }
            else if (Convert.ToInt32(txtFenHu1JH10.Text) < 500)
            {
                txtFenHu1JH10.Focus();
                msg = "单月计划量最低为500";
                return false;
            }
            if (!int.TryParse(txtFenHu1JH11.Text, out val))
            {
                txtFenHu1JH11.Focus();
                msg = "分户1月计划信息输入错误";
                return false;
            }
            else if (Convert.ToInt32(txtFenHu1JH11.Text) < 500)
            {
                txtFenHu1JH11.Focus();
                msg = "单月计划量最低为500";
                return false;
            }
            if (!int.TryParse(txtFenHu1JH12.Text, out val))
            {
                txtFenHu1JH12.Focus();
                msg = "分户1月计划信息输入错误";
                return false;
            }
            else if (Convert.ToInt32(txtFenHu1JH12.Text) < 500)
            {
                txtFenHu1JH12.Focus();
                msg = "单月计划量最低为500";
                return false;
            }
            return true;
        }

        public void ClearInfo()
        {
            ControlCollection controls = tabUserInfo.Controls;
            ControlCollection controlsjh = tabJiHua.Controls;
            foreach (Control c in controls)
            {
                if (c is DevExpress.Web.ASPxTextBox)
                {
                    DevExpress.Web.ASPxTextBox txt = c as DevExpress.Web.ASPxTextBox;
                    txt.Text = "";
                }
                else if (c is DevExpress.Web.ASPxComboBox)
                {
                    DevExpress.Web.ASPxComboBox com = c as DevExpress.Web.ASPxComboBox;
                    com.SelectedIndex = -1;
                }
                else if (c is DevExpress.Web.ASPxCheckBox)
                {
                    DevExpress.Web.ASPxCheckBox check = c as DevExpress.Web.ASPxCheckBox;
                    check.Checked = false;
                }
            }
            foreach (Control c in controlsjh)
            {
                if (c is DevExpress.Web.ASPxTextBox)
                {
                    DevExpress.Web.ASPxTextBox txt = c as DevExpress.Web.ASPxTextBox;
                    txt.Text = "";
                }
            }
        }

        /// <summary>
        /// 根据来函信息加载分户内容
        /// </summary>
        /// <param name="yonghuh">用户号</param>
        public void LoadInfo(YS_LH_LAIHANXX laihanxx)
        {
            if (laihanxx == null || laihanxx.ID == 0)
            {
                this.LaiHanLC = eLaiHanLCBH.未登记;
                SetDisplay();
                return;
            }
            else
            {
                this.LaiHanLC = (eLaiHanLCBH)Enum.Parse(typeof(eLaiHanLCBH), laihanxx.I_LIUCHENGBH.ToString());
                SetDisplay();
            }

            //修改时绑定用户信息
            this.txtYYongHuH.Text = laihanxx.S_YONGHUBH;
            this.txtLaiHanBH.Text = laihanxx.S_LAIHANBH;
            this.LaiHanID = laihanxx.ID.ToString();
            this.LaiHanBH = (eLaiHanLCBH)laihanxx.I_LIUCHENGBH;
            hdCollection.Set("LaiHanBH", laihanxx.S_LAIHANBH);

            //获取原用户信息
            string wherecase = " S_YONGHUH='{0}' and i_jlzt=0 and i_leixing=0 and (S_ZHANDIANBH='{1}' or '00'='{1}') ";
            wherecase = string.Format(wherecase, laihanxx.S_YONGHUBH, BusinessTool.ZHANDIANBH());
            dtYYongHu = bllYongHu.SelectYS_YG_YONGHUXXesDynamic(wherecase, "", null);
            if (dtYYongHu == null || dtYYongHu.Rows.Count == 0)
            {
                PageTools.AlertMsg(this, "用户信息不存在", "window.close();");
                return;
            }
            hdCollection.Set("YongHuH", dtYYongHu.Rows[0]["S_YongHuH"]);
            hdCollection.Set("GUANLIK", dtYYongHu.Rows[0]["S_GUANLIKBH"]);
            hdCollection.Set("ZHANDIAN", dtYYongHu.Rows[0]["S_ZHANDIANBH"]);
            SetYongHuXX(dtYYongHu);

            //绑定分户信息
            this.dtSource = _spHelper.P_YSLH_GETFENHUXX(Convert.ToInt32(laihanxx.I_LAIHANLX),laihanxx.S_LAIHANBH).Tables[0];
            this.gvList.DataSource = this.dtSource;
            this.gvList.DataBind();
            gvList.FocusedRowIndex = -1;

            //绑定计划信息
            SetJiHuaXX(laihanxx.S_YONGHUBH);
        }

        /// <summary>
        /// 加载用户信息
        /// </summary>
        /// <param name="type">0:输入用户回车验证用户号 1：加载用户信息</param>
        /// <returns></returns>
        private string LoadYongHuXX(int type)
        {
            string YongHuH = txtYYongHuH.Text.Trim();
            //用户号是否存在
            if (YongHuH.Length == 0)
            {
                return "-1|查询用户号不能为空";
            }
            //获取用户信息
            string wherecase = " S_YONGHUH='{0}' and i_jlzt=0 and i_leixing=0 and (S_ZHANDIANBH='{1}' or '00'='{1}') ";
            wherecase = string.Format(wherecase, YongHuH, BusinessTool.ZHANDIANBH());
            dtYYongHu = bllYongHu.SelectYS_YG_YONGHUXXesDynamic(wherecase, "", null);
            if (dtYYongHu == null || dtYYongHu.Rows.Count == 0)
            {
                return "-1|查询不到该用户信息";
            }
            //当前水量年月是否有其他来函
            int waterYM = BusinessTool.GetWaterYM();
            int y = Convert.ToInt32(waterYM.ToString().Substring(0, 4));
            int m = Convert.ToInt32(waterYM.ToString().Substring(4, 2));
            DataTable dtlh = bllLaiHanXX.SelectYS_LH_LAIHANXXesDynamic(string.Format(" S_YONGHUBH='{0}' and I_Y={1} and I_M={2} and I_LAIHANLX= " + (int)this.LaiHanLX, YongHuH, y, m), "", null);
            if (dtlh != null && dtlh.Rows.Count > 0)
            {
                return string.Format("-2|该用户来函已存在，来函编号：{0}", dtlh.Rows[0]["S_LAIHANBH"]);
            }
            //是否有水表信息
            string where = " S_SHUIBIAOBH in (select S_SHUIBIAOBH from YS_CB_YONGHUSBGX a where a.S_YONGHUBH='" + YongHuH + "' ) and i_leixing=0 and i_jlzt=0";
            DataTable dt = bllShuiBiaoXX.SelectYS_CB_SHUIBIAOXXesDynamic(where, "", null);
            if ((dt == null || dt.Rows.Count == 0) && (!hdCollection.Contains("IsTrue") || hdCollection.Get("IsTrue").ToString() == "0"))
            {
                return "-3|无用户相关水表";
            }
            if (dt != null && dt.Rows.Count == 1 && (!hdCollection.Contains("IsTrue") || hdCollection.Get("IsTrue").ToString() == "0"))
            {
                return "-3|用户相关水表只存在"+ dt.Rows.Count.ToString() + "个";
            }
            hdCollection.Set("YongHuH", dtYYongHu.Rows[0]["S_YongHuH"]);
            hdCollection.Set("GUANLIK", dtYYongHu.Rows[0]["S_GUANLIKBH"]);
            hdCollection.Set("ZHANDIAN", dtYYongHu.Rows[0]["S_ZHANDIANBH"]);
            if (type == 1)
            {
                SetYongHuXX(dtYYongHu);
                SetJiHuaXX(YongHuH);
            }
            return "1|用户信息查询成功";
        }

        private void SetYongHuXX(DataTable dt)
        {
            if (dt != null && dt.Rows.Count > 0)
            {
                DataRow dr = dt.Rows[0];
                string HeTongH = _spHelper.P_YS_GETHETONGH(dr["S_YONGHUH"].ToString()).Tables[0].Rows[0][0].ToString();

                txtYongHuH.Text = "";
                txtYongHuM.Text = dr["S_YONGHUM"].ToString();
                txtDiZhi.Text = dr["S_DIZHI"].ToString();
                cmbJu.Value = dr["S_JUBIANH"].ToString();
                LoadGongSiByJu();
                cmbGongSi.Value = dr["S_GONGSIBH"].ToString();
                cmbXingZhengQ.Value = dr["S_XINGZHENGQBH"].ToString();
                cmbYWY.Value = dr["S_YEWUY"].ToString();
                txtLianXiR.Text = dr["S_LIANXIR"].ToString();
                txtLianXiDH.Text = dr["S_LIANXIDH"].ToString();
                txtLianXiBM.Value = dr["S_LIANXIBM"].ToString();
                txtYouBian.Text = dr["S_YOUBIAN"].ToString();
                cmbYongShuiXZ.Value = dr["S_YONGSHUIXZ"].ToString();
                cmbHangYeLX.Value = dr["S_SHESHUIDXDM"].ToString();
                txtYSJ.Text = dr["N_YSJ"].ToString();
                cmbKaoHeFY.Value = dr["I_KAOHEFY"].ToString();
                txtKaiHuM.Text = dr["S_KAIHUHM"].ToString();
                txtKaiHuYH.Text = dr["S_KAIHUYH"].ToString();
                txtKaiHuZH.Text = dr["S_YINHANGZH"].ToString();
                if (dr["I_SHOUFEIFS"].ToString() == "1")
                {
                    cbTuoShou.Checked = true;
                }
                this.txtHeTongH.Text = HeTongH;
            }
            else
            {
                ClearInfo();
            }
        }
        /// <summary>
        /// 根据用户信息查询计划信息
        /// </summary>
        /// <param name="YongHuH"></param>
        public void SetJiHuaXX(string YongHuH)
        {
            int waterYM = BusinessTool.GetWaterYM();
            int y = Convert.ToInt32(waterYM.ToString().Substring(0, 4));
            string where = " S_YONGHUH='{0}' and I_Y={1} and i_jlzt=0 ";
            where = string.Format(where, YongHuH, y);
            DataTable dt = bllYuanJH.SelectYS_JH_YUANJIHUAsDynamic(where, "", null);
            int i_m1 = 0, i_m2 = 0, i_m3 = 0, i_m4 = 0, i_m5 = 0, i_m6 = 0, i_m7 = 0, i_m8 = 0, i_m9 = 0, i_m10 = 0, i_m11 = 0, i_m12 = 0, i_m13 = 0;
            if (dt != null && dt.Rows.Count > 0)
            {
                DataRow dr = dt.Rows[0];
                //原计划
                txtFenHuYJH1.Text = dr["I_M1"].ToString();
                txtFenHuYJH2.Text = dr["I_M2"].ToString();
                txtFenHuYJH3.Text = dr["I_M3"].ToString();
                txtFenHuYJH4.Text = dr["I_M4"].ToString();
                txtFenHuYJH5.Text = dr["I_M5"].ToString();
                txtFenHuYJH6.Text = dr["I_M6"].ToString();
                txtFenHuYJH7.Text = dr["I_M7"].ToString();
                txtFenHuYJH8.Text = dr["I_M8"].ToString();
                txtFenHuYJH9.Text = dr["I_M9"].ToString();
                txtFenHuYJH10.Text = dr["I_M10"].ToString();
                txtFenHuYJH11.Text = dr["I_M11"].ToString();
                txtFenHuYJH12.Text = dr["I_M12"].ToString();
                txtFenHuYJH13.Text = (Convert.ToInt32(dr["I_M1"]) + Convert.ToInt32(dr["I_M2"]) + Convert.ToInt32(dr["I_M3"])
                                    + Convert.ToInt32(dr["I_M4"]) + Convert.ToInt32(dr["I_M5"]) + Convert.ToInt32(dr["I_M6"])
                                    + Convert.ToInt32(dr["I_M7"]) + Convert.ToInt32(dr["I_M8"]) + Convert.ToInt32(dr["I_M9"])
                                    + Convert.ToInt32(dr["I_M10"]) + Convert.ToInt32(dr["I_M11"]) + Convert.ToInt32(dr["I_M12"])).ToString();
                //分户计划
                txtFenHu1JH1.Text = dr["I_M1"].ToString();
                txtFenHu1JH2.Text = dr["I_M2"].ToString();
                txtFenHu1JH3.Text = dr["I_M3"].ToString();
                txtFenHu1JH4.Text = dr["I_M4"].ToString();
                txtFenHu1JH5.Text = dr["I_M5"].ToString();
                txtFenHu1JH6.Text = dr["I_M6"].ToString();
                txtFenHu1JH7.Text = dr["I_M7"].ToString();
                txtFenHu1JH8.Text = dr["I_M8"].ToString();
                txtFenHu1JH9.Text = dr["I_M9"].ToString();
                txtFenHu1JH10.Text = dr["I_M10"].ToString();
                txtFenHu1JH11.Text = dr["I_M11"].ToString();
                txtFenHu1JH12.Text = dr["I_M12"].ToString();
                txtFenHu1JH13.Text = (Convert.ToInt32(dr["I_M1"]) + Convert.ToInt32(dr["I_M2"]) + Convert.ToInt32(dr["I_M3"])
                                    + Convert.ToInt32(dr["I_M4"]) + Convert.ToInt32(dr["I_M5"]) + Convert.ToInt32(dr["I_M6"])
                                    + Convert.ToInt32(dr["I_M7"]) + Convert.ToInt32(dr["I_M8"]) + Convert.ToInt32(dr["I_M9"])
                                    + Convert.ToInt32(dr["I_M10"]) + Convert.ToInt32(dr["I_M11"]) + Convert.ToInt32(dr["I_M12"])).ToString();               
            }
            //其他分户合计计划
            if (this.dtSource != null && this.dtSource.Rows.Count > 0)
            {
                for (int i = 0; i < this.dtSource.Rows.Count; i++)
                {
                    i_m1 = i_m1 + Convert.ToInt32(dtSource.Rows[i]["I_M1"].ToString());
                    i_m2 = i_m2 + Convert.ToInt32(dtSource.Rows[i]["I_M2"].ToString());
                    i_m3 = i_m3 + Convert.ToInt32(dtSource.Rows[i]["I_M3"].ToString());
                    i_m4 = i_m4 + Convert.ToInt32(dtSource.Rows[i]["I_M4"].ToString());
                    i_m5 = i_m5 + Convert.ToInt32(dtSource.Rows[i]["I_M5"].ToString());
                    i_m6 = i_m6 + Convert.ToInt32(dtSource.Rows[i]["I_M6"].ToString());
                    i_m7 = i_m7 + Convert.ToInt32(dtSource.Rows[i]["I_M7"].ToString());
                    i_m8 = i_m8 + Convert.ToInt32(dtSource.Rows[i]["I_M8"].ToString());
                    i_m9 = i_m9 + Convert.ToInt32(dtSource.Rows[i]["I_M9"].ToString());
                    i_m10 = i_m10 + Convert.ToInt32(dtSource.Rows[i]["I_M10"].ToString());
                    i_m11 = i_m11 + Convert.ToInt32(dtSource.Rows[i]["I_M11"].ToString());
                    i_m12 = i_m12 + Convert.ToInt32(dtSource.Rows[i]["I_M12"].ToString());
                }
            }
            txtQTHeJiJH1.Text = i_m1.ToString();
            txtQTHeJiJH2.Text = i_m2.ToString();
            txtQTHeJiJH3.Text = i_m3.ToString();
            txtQTHeJiJH4.Text = i_m4.ToString();
            txtQTHeJiJH5.Text = i_m5.ToString();
            txtQTHeJiJH6.Text = i_m6.ToString();
            txtQTHeJiJH7.Text = i_m7.ToString();
            txtQTHeJiJH8.Text = i_m8.ToString();
            txtQTHeJiJH9.Text = i_m9.ToString();
            txtQTHeJiJH10.Text = i_m10.ToString();
            txtQTHeJiJH11.Text = i_m11.ToString();
            txtQTHeJiJH12.Text = i_m12.ToString();
            txtQTHeJiJH13.Text = (i_m1 + i_m2 + i_m3 + i_m4 + i_m5 + i_m6 + i_m7 + i_m8 + i_m9 + i_m10 + i_m11 + i_m12).ToString();
            //合计计划=分户计划+其他分户合计计划
            txtHeJiJH1.Text = (Convert.ToInt32(txtFenHu1JH1.Text==""?"0": txtFenHu1JH1.Text) + i_m1).ToString();
            txtHeJiJH2.Text = (Convert.ToInt32(txtFenHu1JH2.Text == "" ? "0" : txtFenHu1JH2.Text) + i_m2).ToString();
            txtHeJiJH3.Text = (Convert.ToInt32(txtFenHu1JH3.Text == "" ? "0" : txtFenHu1JH3.Text) + i_m3).ToString();
            txtHeJiJH4.Text = (Convert.ToInt32(txtFenHu1JH4.Text == "" ? "0" : txtFenHu1JH4.Text) + i_m4).ToString();
            txtHeJiJH5.Text = (Convert.ToInt32(txtFenHu1JH5.Text == "" ? "0" : txtFenHu1JH5.Text) + i_m5).ToString();
            txtHeJiJH6.Text = (Convert.ToInt32(txtFenHu1JH6.Text == "" ? "0" : txtFenHu1JH6.Text) + i_m6).ToString();
            txtHeJiJH7.Text = (Convert.ToInt32(txtFenHu1JH7.Text == "" ? "0" : txtFenHu1JH7.Text) + i_m7).ToString();
            txtHeJiJH8.Text = (Convert.ToInt32(txtFenHu1JH8.Text == "" ? "0" : txtFenHu1JH8.Text) + i_m8).ToString();
            txtHeJiJH9.Text = (Convert.ToInt32(txtFenHu1JH9.Text == "" ? "0" : txtFenHu1JH9.Text) + i_m9).ToString();
            txtHeJiJH10.Text = (Convert.ToInt32(txtFenHu1JH10.Text == "" ? "0" : txtFenHu1JH10.Text) + i_m10).ToString();
            txtHeJiJH11.Text = (Convert.ToInt32(txtFenHu1JH11.Text == "" ? "0" : txtFenHu1JH11.Text) + i_m11).ToString();
            txtHeJiJH12.Text = (Convert.ToInt32(txtFenHu1JH12.Text == "" ? "0" : txtFenHu1JH12.Text) + i_m12).ToString();
            txtHeJiJH13.Text = (Convert.ToInt32(txtHeJiJH1.Text) + Convert.ToInt32(txtHeJiJH2.Text) + Convert.ToInt32(txtHeJiJH3.Text)
                                + Convert.ToInt32(txtHeJiJH4.Text) + Convert.ToInt32(txtHeJiJH5.Text) + Convert.ToInt32(txtHeJiJH6.Text)
                                + Convert.ToInt32(txtHeJiJH7.Text) + Convert.ToInt32(txtHeJiJH8.Text) + Convert.ToInt32(txtHeJiJH9.Text)
                                + Convert.ToInt32(txtHeJiJH10.Text) + Convert.ToInt32(txtHeJiJH11.Text) + Convert.ToInt32(txtHeJiJH12.Text)).ToString();

        }

        //登记时获取来函信息
        public YS_LH_LAIHANXX GetLaiHanXX(bool isadd, int liuchengbh)
        {
            YS_LH_LAIHANXX lhxx = new YS_LH_LAIHANXX();
            if (isadd)
            {
                int waterYM = BusinessTool.GetWaterYM();
                int y = Convert.ToInt32(waterYM.ToString().Substring(0, 4));
                int m = Convert.ToInt32(waterYM.ToString().Substring(4, 2));
                lhxx.ID = 0;
                lhxx.D_CAOZUOSJ = DateTime.Now;
                lhxx.D_LAIHANRQ = DateTime.Now;
                lhxx.D_SHURURQ = DateTime.Now;
                lhxx.I_AUTOTYPE = 0;
                lhxx.I_JLZT = 0;
                lhxx.I_LAIHANLX = 8;
                lhxx.I_LIUCHENGZT = 0;
                lhxx.I_M = m;
                lhxx.I_SHANGCHUAN = 0;
                lhxx.I_SHURUR = LoginInfo.CurrentUser.UserID;
                lhxx.I_XIANCHUAN = 0;
                lhxx.I_Y = y;
                lhxx.I_TZY = y;
                string xukebh = "";
                string laihanbh = "";
                string zd, glk, ywy;
                PageTools.GetLHDJZDGLKYWY(hdCollection.Get("YongHuH").ToString(), out zd, out glk, out ywy);
                lhxx.S_ZHANDIANBH = zd;
                lhxx.S_GUANLIKBH = glk;
                lhxx.I_YWY = int.Parse(ywy);
                lhxx.S_YWY = BusinessTool.GetYWYName(ywy);
                DataSet numset = _spHelper.P_YS_LH_XUKEBH(glk, 8, 0, y.ToString(), out xukebh, out laihanbh);
                if (hdCollection.Contains("LaiHanBH"))
                {
                    laihanbh = hdCollection.Get("LaiHanBH").ToString();
                }
                if (hdCollection.Contains("XuKeBH"))
                {
                    xukebh = hdCollection.Get("XuKeBH").ToString();
                }
                lhxx.S_CAOZUOR = LoginInfo.CurrentUser.UserID.ToString();
                lhxx.S_LAIHANBH = laihanbh;
                lhxx.S_TONGYIBM = "";
                lhxx.S_XUKEBH = xukebh;
                lhxx.S_YONGHUBH = hdCollection.Get("YongHuH").ToString();
                DataRowCollection drcTemp = new YS_YG_YONGHUXXProcess().SelectYS_YG_YONGHUXXesDynamic("I_JLZT=0 AND I_LEIXING=1 AND S_YONGHUH='" + lhxx.S_YONGHUBH + "'", null).Rows;
                lhxx.S_YONGHUM = Convert.ToString(drcTemp[0]["S_YONGHUM"]);
                lhxx.I_XT = 0;
            }
            else
            {
                lhxx = bllLaiHanXX.SelectYS_LH_LAIHANXX(Convert.ToDecimal(LaiHanID));
            }
            lhxx.I_LIUCHENGBH = liuchengbh;
            lhxx.S_CAOZUOR = LoginInfo.CurrentUser.UserID.ToString();
            lhxx.D_CAOZUOSJ = BusinessTool.GetDataBaseDateTime();
            return lhxx;
        }

        //获取分户副表信息
        public void AddOtherTable(YS_LH_LAIHANXX lhxx)
        {
            string fenhuh = "";
            foreach (DataRow dr in dtSource.Rows)
            {
                fenhuh = fenhuh + "'"+dr["S_YONGHUH"].ToString()+"',";
            }
            fenhuh = fenhuh==""?"":fenhuh.Substring(0, fenhuh.Length - 1);

            AddFenHuXX(lhxx, fenhuh);//添加分户副表计划信息
            InsertYongHuXX(lhxx, fenhuh);//添加分户用户信息
            AddLaiHanSBGX(lhxx.S_LAIHANBH, fenhuh);//添加分户水表分配信息

            LaiHanID = lhxx.ID.ToString();
        }
        
        /// <summary>
        /// 添加分户副表计划信息
        /// </summary>
        /// <param name="laihanxx"></param>
        public void AddFenHuXX(YS_LH_LAIHANXX laihanxx,string fenhuh)
        {
            OperateState operateState = OperateState.Add;
            int waterYM = BusinessTool.GetWaterYM();
            int y = Convert.ToInt32(waterYM.ToString().Substring(0, 4));

            //删除多余分户
            string delwhere = "  S_LAIHANBH='{0}' and S_YONGHUH not in({1}) ";
            delwhere = string.Format(delwhere, laihanxx.S_LAIHANBH, fenhuh);
            //bllFenHuXX.UpdateYS_LH_FENHUXXesDynamic(" i_jlzt=-1 ", delwhere);
            bllFenHuXX.DeleteYS_LH_FENHUXXesDynamic(delwhere);

            foreach (DataRow dr in dtSource.Rows)
            {
                YS_LH_FENHUXX fhxx = new YS_LH_FENHUXX();
                fhxx.D_CAOZUORQ = DateTime.Now;
                fhxx.I_Y = y;
                fhxx.ID = 0;
                fhxx.I_JLZT = 0;
                fhxx.S_CAOZUOR = LoginInfo.CurrentUser.UserID.ToString();
                fhxx.S_YONGHUH = dr["S_YONGHUH"].ToString();
                fhxx.S_LAIHANBH = laihanxx.S_LAIHANBH;
                fhxx.S_YONGHUHFATHER = laihanxx.S_YONGHUBH;
                fhxx.I_M1 = Convert.ToInt32(dr["I_M1"].ToString());
                fhxx.I_M2 = Convert.ToInt32(dr["I_M2"].ToString());
                fhxx.I_M3 = Convert.ToInt32(dr["I_M3"].ToString());
                fhxx.I_M4 = Convert.ToInt32(dr["I_M4"].ToString());
                fhxx.I_M5 = Convert.ToInt32(dr["I_M5"].ToString());
                fhxx.I_M6 = Convert.ToInt32(dr["I_M6"].ToString());
                fhxx.I_M7 = Convert.ToInt32(dr["I_M7"].ToString());
                fhxx.I_M8 = Convert.ToInt32(dr["I_M8"].ToString());
                fhxx.I_M9 = Convert.ToInt32(dr["I_M9"].ToString());
                fhxx.I_M10 = Convert.ToInt32(dr["I_M10"].ToString());
                fhxx.I_M11 = Convert.ToInt32(dr["I_M11"].ToString());
                fhxx.I_M12 = Convert.ToInt32(dr["I_M12"].ToString());

                string where = " S_LAIHANBH='{0}' and S_YONGHUH='{1}' and S_YONGHUHFATHER='{2}' and i_JLZT=0 ";
                where = string.Format(where, laihanxx.S_LAIHANBH, dr["S_YONGHUH"].ToString(), laihanxx.S_YONGHUBH);
                DataTable dt = bllFenHuXX.SelectYS_LH_FENHUXXesDynamic(where, "", null);
                if (dt != null && dt.Rows.Count > 0)
                {
                    fhxx.ID = Convert.ToInt32(dt.Rows[0]["ID"]);
                    operateState = OperateState.Modify;
                }

                bllFenHuXX.InsertOrUpdate(fhxx);
                _SYS_LOGBLL.Log<YS_LH_FENHUXX>(LoginInfo.CurrentUser, BusinessTool.WType.ToString(), "来函管理-分户流程", operateState, fhxx, null);             
            }
        }

        /// <summary>
        /// 插入用户调整信息
        /// </summary>
        /// <param name="laihanxx"></param>
        public void InsertYongHuXX(YS_LH_LAIHANXX laihanxx, string fenhuh)
        {
            OperateState operateState = OperateState.Add;
            string laihanbh = laihanxx.S_LAIHANBH;

            //删除多余分户
            string delwhere = "  S_LAIHANBH='{0}' and S_YONGHUH not in({1}) ";
            delwhere = string.Format(delwhere, laihanxx.S_LAIHANBH, fenhuh);
            //bllLaiHanYHTZ.UpdateYS_LH_YONGHUTZsDynamic(" i_jlzt=-1 ", delwhere);
            bllLaiHanYHTZ.DeleteYS_LH_YONGHUTZsDynamic(delwhere);

            foreach (DataRow dr in dtSource.Rows)
            {
                YS_LH_YONGHUTZ yhxx = new YS_LH_YONGHUTZ();
                yhxx.D_CAOZUOSJ = DateTime.Now;
                yhxx.I_JLZT = 0;
                yhxx.I_LEIXING = 0;
                yhxx.I_YONGHUZT = 0;
                yhxx.ID = 0;
                yhxx.I_YUANYONGH = 1;
                yhxx.S_CAOZUOR = LoginInfo.CurrentUser.UserID.ToString();
                yhxx.S_YUANYONGHUH = hdCollection.Get("YongHuH").ToString();
                yhxx.S_GUANLIKBH = hdCollection.Get("GUANLIK").ToString();
                yhxx.S_ZHANDIANBH = hdCollection.Get("ZHANDIAN").ToString();
                yhxx.S_LAIHANBH = laihanbh;

                yhxx.S_YONGHUH = dr["S_YONGHUH"].ToString();
                yhxx.S_YONGHUM = dr["S_YONGHUM"].ToString();
                yhxx.S_DIZHI = dr["S_DIZHI"].ToString();
                yhxx.S_XINGZHENGQBH = dr["S_XINGZHENGQBH"].ToString();
                yhxx.S_JUBIANH = dr["S_JUBIANH"].ToString();
                yhxx.S_GONGSIBH = dr["S_GONGSIBH"].ToString();
                yhxx.S_YEWUY = dr["S_YEWUY"].ToString();
                yhxx.S_LIANXIR = dr["S_LIANXIR"].ToString();
                yhxx.S_LIANXIDH = dr["S_LIANXIDH"].ToString();
                yhxx.S_LIANXIBM = dr["S_LIANXIBM"].ToString();
                yhxx.S_YOUBIAN = dr["S_YOUBIAN"].ToString();
                yhxx.S_YONGSHUIXZ = dr["S_YONGSHUIXZ"].ToString();
                yhxx.S_SHESHUIDXDM = dr["S_SHESHUIDXDM"].ToString();
                yhxx.N_YSJ = Convert.ToDouble(dr["N_YSJ"]);
                yhxx.I_KAOHEFY = Convert.ToInt32(dr["I_KAOHEFY"]);
                yhxx.S_KAIHUHM = dr["S_KAIHUHM"].ToString();
                yhxx.S_KAIHUYH = dr["S_KAIHUYH"].ToString();
                yhxx.S_YINHANGZH = dr["S_YINHANGZH"].ToString();
                yhxx.I_SHOUFEIFS = Convert.ToInt32(dr["I_SHOUFEIFS"]);
                yhxx.S_BEIYONG5 = dr["S_HETONGH"].ToString();

                string where = " S_LAIHANBH='{0}' and S_YONGHUH='{1}' and i_jlzt=0  ";
                where = string.Format(where, laihanbh, yhxx.S_YONGHUH);
                DataTable dt = bllLaiHanYHTZ.SelectYS_LH_YONGHUTZsDynamic(where, null, null);
                if (dt != null && dt.Rows.Count > 0)
                {
                    yhxx.ID = Convert.ToInt32(dt.Rows[0]["ID"]);
                    operateState = OperateState.Modify;
                }

                bllLaiHanYHTZ.InsertOrUpdate(yhxx);
                _SYS_LOGBLL.Log<YS_LH_YONGHUTZ>(LoginInfo.CurrentUser, BusinessTool.WType.ToString(), "来函管理-分户流程", operateState, yhxx, null);               
            }
        }

        /// <summary>
        /// 插入水表分配信息
        /// </summary>
        /// <param name="laihanbh"></param>
        public void AddLaiHanSBGX(string laihanbh, string fenhuh)
        {
            OperateState operateState = OperateState.Add;
            if (Session["DtFenHuSB"] != null)
            {
                DataTable dtFenHuSB = Session["DtFenHuSB"] as DataTable;
                if (dtFenHuSB != null && dtFenHuSB.Rows.Count > 0)
                {
                    //删除多余分户
                    string delwhere = "  S_LAIHANBH='{0}' and S_YONGHUBH not in({1}) ";
                    delwhere = string.Format(delwhere, laihanbh, fenhuh);
                    //bllYongHuSBGX.UpdateYS_LH_YONGHUSBGXesDynamic("I_JLZT=-1", delwhere);
                    bllYongHuSBGX.DeleteYS_LH_YONGHUSBGXesDynamic(delwhere);

                    foreach (DataRow dr in dtFenHuSB.Rows)
                    {
                        YS_LH_YONGHUSBGX sbgx = new YS_LH_YONGHUSBGX();
                        sbgx.D_CAOZUOSJ = DateTime.Now;
                        sbgx.I_JLZT = 0;
                        sbgx.I_LEIXING = 0;
                        sbgx.I_SHUIBIAOZT = 0;
                        sbgx.ID = 0;
                        sbgx.S_CAOZUOR = LoginInfo.CurrentUser.UserID.ToString();
                        sbgx.S_LAIHANBH = laihanbh;
                        sbgx.S_SHUIBIAOBH = dr["S_SHUIBIAOBH"].ToString();
                        sbgx.S_YONGHUBH = dr["S_YONGHUBH"].ToString();
                        sbgx.S_ZHANDIANBH = dr["S_ZHANDIANBH"].ToString();

                        string where = " S_LAIHANBH='{0}' and S_YONGHUBH='{1}' and i_jlzt=0 ";
                        string wherecase = string.Format(where, laihanbh, sbgx.S_YONGHUBH);
                        DataTable dt = bllYongHuSBGX.SelectYS_LH_YONGHUSBGXesDynamic(wherecase, "", null);
                        if (dt != null && dt.Rows.Count > 0)
                        {
                            sbgx.ID = Convert.ToInt32(dt.Rows[0]["ID"]);
                            operateState = OperateState.Modify;
                        }

                        bllYongHuSBGX.InsertOrUpdate(sbgx);
                        _SYS_LOGBLL.Log<YS_LH_YONGHUSBGX>(LoginInfo.CurrentUser, BusinessTool.WType.ToString(), "来函管理-分户流程", operateState, sbgx, null);
                    }
                }
            }
        }
        #endregion
    }
}